<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>WeakReference 类</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="fiber.getcurrent.html">« Fiber::getCurrent</a></li>
      <li style="float: right;"><a href="weakreference.construct.html">WeakReference::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="reserved.interfaces.html">预定义接口和类</a></li>
    <li>WeakReference 类</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.weakreference" class="reference">

 <h1 class="title">WeakReference 类</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 7 &gt;= 7.4.0, PHP 8)</p>


  <div class="section" id="weakreference.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    弱引用可以指向一个对象，并且不阻止对象的销毁。可以实现具有对象结构的缓存。
   </p>
   <p class="para">
    <span class="classname"><strong class="classname">弱引用类</strong></span>不能序列化。
   </p>
  </div>


  <div class="section" id="weakreference.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">final</span>
      <span class="modifier">class</span> <strong class="classname">WeakReference</strong>
     </span>
     {</div>
    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="weakreference.construct.html" class="methodname">__construct</a></span>()</div>

    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="weakreference.create.html" class="methodname">create</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type"><a href="class.weakreference.html" class="type WeakReference">WeakReference</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakreference.get.html" class="methodname">get</a></span>(): <span class="type"><span class="type">?</span><span class="type">object</span></span></div>

   }</div>


  </div>

  <div class="section" id="weakreference.examples">
   <h2 class="title">弱引用示例</h2>
   <p class="para">
    <div class="example" id="weakreference.basic-example">
     <p><strong>示例 #1 弱引用的基础用法</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$weakref&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">WeakReference</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$weakref</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">());<br />unset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$weakref</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程的输出类似于：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (0) {
}
NULL
</pre></div>
     </div>
    </div>
   </p>
  </div>

 </div>

 







 






 








<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="weakreference.construct.html">WeakReference::__construct</a> — 不允许实例化的构造函数</li><li><a href="weakreference.create.html">WeakReference::create</a> — 创建新的弱引用</li><li><a href="weakreference.get.html">WeakReference::get</a> — 获取弱引用对象</li></ul>
</div>
</div></div></body></html>